﻿<UserControl x:Class="Notoric.Gui.Controls.ZoomViewer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:ls="clr-namespace:LukasKopenec.LocalizationSupport;assembly=LocalizationSupport"
             xmlns:cvr="clr-namespace:Notoric.Gui.Converters"
             xmlns:vme="clr-namespace:Notoric.ViewModel.Elements;assembly=ViewModel"
             xmlns:my="clr-namespace:Notoric.Gui.Controls"
             MinWidth="100" MinHeight="50"
             x:Name="parentControl"
             DataContextChanged="parentControl_DataContextChanged">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../Resources/Templates/Section.xaml"/>
                <ResourceDictionary>
                    <cvr:PercentageConverter x:Key="PercentageConverter"/>
                    <cvr:ZoomFormatConverter x:Key="ZoomFormatConverter"/>

                    <!-- Slider style -->
                    <Style x:Key="SliderButtonStyle" TargetType="{x:Type RepeatButton}">
                        <Setter Property="SnapsToDevicePixels" Value="true"/>
                        <Setter Property="OverridesDefaultStyle" Value="true"/>
                        <Setter Property="IsTabStop" Value="false"/>
                        <Setter Property="Focusable" Value="false"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type RepeatButton}">
                                    <Border Background="Transparent" />
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

                    <Style x:Key="SliderThumbStyle" TargetType="{x:Type Thumb}">
                        <Setter Property="SnapsToDevicePixels" Value="true"/>
                        <Setter Property="OverridesDefaultStyle" Value="true"/>
                        <Setter Property="Height" Value="14"/>
                        <Setter Property="Width" Value="14"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type Thumb}">
                                    <Ellipse 
                                      Name="Ellipse" 
                                      Fill="{StaticResource NormalBrush}"
                                      Stroke="{StaticResource NormalBorderBrush}"
                                      StrokeThickness="1" />
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter TargetName="Ellipse" Property="Fill" Value="{StaticResource DarkBrush}"/>
                                        </Trigger>
                                        <Trigger Property="IsEnabled" Value="false">
                                            <Setter TargetName="Ellipse" Property="Fill" Value="{StaticResource DisabledBackgroundBrush}"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

                    <ControlTemplate x:Key="HorizontalSlider" TargetType="{x:Type Slider}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto" MinHeight="{TemplateBinding Slider.MinHeight}"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <TickBar 
                              Name="TopTick"
                              SnapsToDevicePixels="True" 
                              Placement="Top"
                              Fill="{StaticResource GlyphBrush}"
                              Height="4"
                              Visibility="Collapsed" />
                            <Border 
                              Name="TrackBackground"
                              Margin="0"
                              CornerRadius="2" 
                              Height="4"
                              Grid.Row="1"
                              Background="{StaticResource LightBrush}" 
                              BorderBrush="{StaticResource NormalBorderBrush}"
                              BorderThickness="1" />
                            <Track Grid.Row="1" Name="PART_Track">
                                <Track.DecreaseRepeatButton>
                                    <RepeatButton 
                                      Style="{StaticResource SliderButtonStyle}"
                                      Command="Slider.DecreaseLarge" />
                                </Track.DecreaseRepeatButton>
                                <Track.Thumb>
                                    <Thumb Style="{StaticResource SliderThumbStyle}" />
                                </Track.Thumb>
                                <Track.IncreaseRepeatButton>
                                    <RepeatButton 
                                      Style="{StaticResource SliderButtonStyle}"
                                      Command="Slider.IncreaseLarge" />
                                </Track.IncreaseRepeatButton>
                            </Track>
                            <TickBar 
                              Name="BottomTick"
                              SnapsToDevicePixels="True" 
                              Grid.Row="2"
                              Fill="{TemplateBinding Foreground}"
                              Placement="Bottom"
                              Height="4"
                              Visibility="Collapsed" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="TickPlacement" Value="TopLeft">
                                <Setter TargetName="TopTick" Property="Visibility" Value="Visible"/>
                            </Trigger>
                            <Trigger Property="TickPlacement" Value="BottomRight">
                                <Setter TargetName="BottomTick" Property="Visibility" Value="Visible"/>
                            </Trigger>
                            <Trigger Property="TickPlacement" Value="Both">
                                <Setter TargetName="TopTick" Property="Visibility" Value="Visible"/>
                                <Setter TargetName="BottomTick" Property="Visibility" Value="Visible"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>

                    <Style TargetType="{x:Type Slider}">
                        <Setter Property="SnapsToDevicePixels" Value="true"/>
                        <Setter Property="OverridesDefaultStyle" Value="true"/>
                        <Style.Triggers>
                            <Trigger Property="Orientation" Value="Horizontal">
                                <Setter Property="MinWidth" Value="104" />
                                <Setter Property="MinHeight" Value="21" />
                                <Setter Property="Template" Value="{StaticResource HorizontalSlider}" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>

                    <!-- Zoom In / Out buttons style. -->
                    <Style TargetType="{x:Type Button}">
                        <Setter Property="SnapsToDevicePixels" Value="True"/>
                        <Setter Property="OverridesDefaultStyle" Value="True"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="Button">
                                    <Grid Width="16" Height="16">
                                        <Ellipse 
                                                Name="Ellipse" 
                                                Fill="{StaticResource NormalBrush}"
                                                Stroke="{StaticResource NormalBorderBrush}"
                                                StrokeThickness="1"
                                                Height="16" Width="16"
                                                />
                                        <TextBlock Name="Symbol" Text="{TemplateBinding Content}" 
                                                       Foreground="{TemplateBinding Foreground}"
                                                       VerticalAlignment="Center" HorizontalAlignment="Center"/>
                                    </Grid>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter TargetName="Ellipse" Property="Fill" Value="{StaticResource DarkBrush}"/>
                                        </Trigger>
                                        <Trigger Property="IsEnabled" Value="false">
                                            <Setter TargetName="Ellipse" Property="Fill" Value="{StaticResource DisabledBackgroundBrush}"/>
                                            <Setter TargetName="Symbol" Property="Foreground" Value="Gray"/>
                                        </Trigger>
                                        <Trigger Property="IsPressed" Value="True">
                                            <Setter TargetName="Ellipse" Property="Fill" Value="{StaticResource PressedBrush}"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

                    <DataTemplate DataType="{x:Type vme:Page}">
                        <my:Page DataContext="{Binding}"/>
                    </DataTemplate>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="40" />
        </Grid.RowDefinitions>
        <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <ContentControl Content="{Binding}" Margin="10">
                <ContentControl.BitmapEffect>
                    <DropShadowBitmapEffect/>
                </ContentControl.BitmapEffect>

                <ContentControl.LayoutTransform>
                    <ScaleTransform ScaleX="{Binding ElementName=zoom,Path=Value,Mode=OneWay,
                        Converter={StaticResource PercentageConverter}}"
                                    ScaleY="{Binding ElementName=zoom,Path=Value,Mode=OneWay,
                        Converter={StaticResource PercentageConverter}}"/>
                </ContentControl.LayoutTransform>
            </ContentControl>
        </ScrollViewer>
        <StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Right">
            <Label x:Name="zoomLabel" VerticalAlignment="Center" 
                   Content="{Binding ElementName=zoom,Path=Value,Mode=OneWay,
                        Converter={StaticResource ZoomFormatConverter}}"
                   IsEnabled="False" Margin="2"/>
            <Button x:Name="zoomMinus" Content="-" VerticalAlignment="Center" Click="zoomMinus_Click"
                    IsEnabled="False" ToolTip="{ls:Localize Application.ZoomOut}"/>
            <Slider x:Name="zoom" VerticalAlignment="Center" Margin="3"
                    Minimum="{Binding ElementName=parentControl,Path=ZoomMin,Mode=OneWay}"
                    Maximum="{Binding ElementName=parentControl,Path=ZoomMax,Mode=OneWay}"
                    Value="{Binding ElementName=parentControl,Path=Zoom,Mode=TwoWay}"
                    IsEnabled="False"/>
            <Button x:Name="zoomPlus" Content="+" VerticalAlignment="Center" Click="zoomPlus_Click"
                    IsEnabled="False" ToolTip="{ls:Localize Application.ZoomIn}"/>
        </StackPanel>
    </Grid>
</UserControl>
